<?php

class openapi_api_function_v1_home extends openapi_api_function_abstract implements openapi_api_function_interface{

    public function index($params,&$code,&$sub_msg)
    {
    	$member_id = $this->checklogin($params['login_token']);
        $store_grade = 0;
        $sub_store_grade = 0;
        if($member_id){
            $store = app::get("business")->model('storemanger')->getRow("store_grade",array('account_id'=>$member_id));
            $store_grade = $store['store_grade'];
            $substore = app::get('business')->model('storegrade')->getRow('grade_id',array('parent_id'=>$store_grade));
            $sub_store_grade = $substore['grade_id'];
        }
        $member = $this->get_current_member();
        $site_member_lv_id = $member['member_lv'];

    	$db = kernel::database();

    	//轮播
		$slider_sql = 'select b.*,i.url from ' . DB_PREFIX . 'cellphone_banner b join ' . DB_PREFIX . 'image_image i on b.image_id=i.image_id where b.is_active="true" and b.start_time<=unix_timestamp() and b.end_time>=unix_timestamp() order by b.d_order asc';
		$sliders = $db->select($slider_sql);
		$result['sliders'] = $sliders;

		//分类
		$cats = app::get('b2c')->model('goods_cat')->getList('cat_id,cat_logo,is_leaf,cat_name,p_order',array('parent_id'=>0,'hidden'=>'false'));
		foreach ($cats as $cat_k => &$cat_v) {
			if(!empty($cat_v['cat_logo'])){
				$img = app::get('image')->model('image')->getList("url",array('image_id'=>$cat_v['cat_logo']));
				$cat_v['cat_logo'] = current($img[0]);
			}
		}
		$result['cats'] = $cats;

		//栏目及栏目下商品
		$col_type_sql = 'select columntype_id,columntype_name,d_order from ' . DB_PREFIX . 'cellphone_columntype order by d_order asc,columntype_createtime desc limit 3';
		$columntype = $db->select($col_type_sql);
		$list2dump = kernel::single("b2c_goods_list2dump");
		if(!empty($columntype)){
			foreach ($columntype as $c_k => &$c_v) {
				$sql = 'select c.column_id,c.col_id,c.column_type,c.d_order,c.columntype_id,c.image_id from ' . DB_PREFIX . 'cellphone_column c where c.columntype_id='.$c_v['columntype_id'].' and c.is_active="true" and c.start_time<=unix_timestamp() and c.end_time>=unix_timestamp() order by c.d_order asc';
				$column = $db->select($sql);
				if(!empty($column)){
					foreach ($column as $col_k => &$col_v) {
						if($col_v['column_type'] == 'goods'){
							$goods = array();
							$goods = app::get('b2c')->model('goods')->getList("goods_id,name,price,cat_id,mktprice,preparesell",array('goods_id'=>$col_v['col_id']));
        					$aGoods = $list2dump->get_goods($goods[0],$site_member_lv_id);
        					$goods[0]['price'] = number_format($aGoods['current_price'],2,".","");
        					$goods[0]['mktprice'] = number_format($aGoods['mktprice'],2,".","");
        					if($goods[0]['price']>=$goods[0]['mktprice']){
        						unset($goods[0]['mktprice']);
        					}
							//是否预售商品
				            if($goods[0]['preparesell']){
				                $pre_sql = "SELECT * FROM " . DB_PREFIX . "preparesell_preparesell_goods where status='true' and prepare_id='".$goods[0]['preparesell']."' and begin_time<unix_timestamp(now()) and end_time>unix_timestamp(now())";
				                $pre = kernel::database()->select($pre_sql);
				                if(!empty($pre)){
				                    $goods[0]['promotion_type'] = "Pre-sale";
				                    if($store_grade){
				                        $pre_price_sql = "SELECT preparesell_price FROM " . DB_PREFIX . "preparesell_preparesell_price where prepare_id=".$pre[0]['prepare_id']." and product_id=".$pre[0]['product_id']." and grade_id=".$store_grade;
				                    }else{
				                        $pre_price_sql = "SELECT max(preparesell_price) preparesell_price FROM " . DB_PREFIX . "preparesell_preparesell_price where prepare_id=".$pre[0]['prepare_id']." and product_id=".$pre[0]['product_id'];
				                    }
				                    $preparesell_price = kernel::database()->select($pre_price_sql);
				                    $goods[0]['promotion_price'] = $preparesell_price[0]['preparesell_price'];
				                    $goods[0]['promotion_desc'] = '';
				                }
				            }
				            unset($goods[0]['preparesell']);
				            $col_v['url'] = kernel::single('base_storager')->image_path($col_v['image_id'],'m');
							$col_v['goods'] = $goods[0];
						}
					}
				}
				$c_v['column'] = $column;
			}
		}
		$result['columntypes'] = $columntype;
		return $this->apiData(true,$result,200);
    }
}